package com.jwt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jwt.mapper.JwtMapper;
import com.jwt.pojo.Jwt;
import com.jwt.service.JwtService;
import com.jwt.utils.JwtTokenUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class JwtServiceImpl implements JwtService {

    @Resource
    private JwtMapper jwtMapper;

    @Autowired
    private JwtTokenUtil jwtUtil;

    @Override
    public Jwt login(Jwt jwt) {
        QueryWrapper<Jwt> jwtQueryWrapper = new QueryWrapper<>();
        jwtQueryWrapper.select("*").eq("username", jwt.getUsername());
        Jwt jwtUser = jwtMapper.selectOne(jwtQueryWrapper);

        //使得前后端可以通话的操作，采用JWT来实现
        String token = jwtUtil.generateToken(jwtUser.getUsername(), "user");
        return null;
    }

    @Override
    public String resolveToken(String token) {
        String username= "";
        if (jwtUtil.isTokenExpired(token) && (username = jwtUtil.getUsernameFromToken(token)) != null){
            jwtUtil.refreshToken(token);
        }
        return username;
    }

}
